package com.attcatstudy.Thread;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-10-31 20:20
 * @description
 */
public class ThreadStopDemo {
    private static Object lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println(Thread.currentThread().getName() + "获取锁");
                    try {
//                        Thread.sleep(60000);  不释放锁
                        lock.wait(5000); //释放锁
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(Thread.currentThread().getName() + "执行完成");
            }
        },"线程1");

        thread.start();
        Thread.sleep(200);
        // 停止thread，并释放锁
        thread.stop();

        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println(Thread.currentThread().getName() + "获取锁");
                    try {
                        Thread.sleep(600);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(Thread.currentThread().getName() + "执行完成");
            }
        },"线程2");

        thread2.start();



        Thread threa3=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    System.out.println(Thread.currentThread().getName() + "获取锁");

                }
                System.out.println(Thread.currentThread().getName() + "执行完成");
            }
        },"线程3");

        threa3.start();


    }

}
